import { chargeOffBillApi, getBillCostsApi, shiftOrOutCostApi } from "@/api/financialManagement/receivable/statement";
import { message, Modal } from "ant-design-vue";
import { computed, createVNode, Ref } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: {
  selectMap: Ref<Base.GeneraDataType<string[]>>;
  customerAccountRef: Ref<any>;
  emit: any;
  billInfo: { depositList: Array<Base.GeneraDataType>; costList: Array<Base.GeneraDataType>; sumUnChargeOffPrice: number; sumReceivablePrice: number };
  passData: Ref<WaterVerification.ListRows[]>;
  loading: Ref<boolean>;
  searchForm: ReceivableStatement.BillCostrSearchForm;
  dialogOpts: Base.DialogOpts;
  type: Ref<number>;
}) {
  const { dialogOpts, searchForm, loading, passData, billInfo, selectMap, customerAccountRef, emit } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return customerAccountRef.value.getCheckboxRecords() || [];
  });
  const submitHandle = () => {
    if (!selectRows.value.length) return message.error("请先选择流水!");
    const depositIds = selectRows.value.map((t: any) => t.id);
    const billCosts = [];
    for (const key in selectMap.value) {
      const target = selectMap.value[key];
      billCosts.push({
        receivableBillId: key,
        costIds: target.map((t: any) => t.id)
      });
    }
    const params = {
      depositIds,
      billCosts,
      customerId: passData.value[0].customerId
    };
    return chargeOffBillApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("核销成功!");
        emit("submit");
      }
    });
  };
  const searchHandle = () => {
    loading.value = true;
    const params: any = {
      ...searchForm,
      customerId: passData.value[0].customerId,
      channelId: searchForm.channelId.length ? searchForm.channelId[1] : ""
    };
    if (params.waybillNums) {
      params.waybillNums = params.waybillNums
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    } else {
      params.waybillNums = [];
    }
    return getBillCostsApi(params as any)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          billInfo.costList = data.list || [];
          billInfo.sumReceivablePrice = data.sumReceivablePrice || 0;
          billInfo.sumUnChargeOffPrice = data.sumUnChargeOffPrice || 0;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const selectChangeEvent = (e: any, row: any) => {
    selectMap.value[row.id] = e.records;
  };
  const selectAllEvent = (e: any, row: any) => {
    selectMap.value[row.id] = e.records;
  };
  // 移出费用
  const outCostHandle = () => {
    const params: ReceivableStatement.ShiftOrOutCostForm = {
      isShiftIN: 2,
      costIds: [],
      receivableBillId: null as unknown as string
    };
    for (const key in selectMap.value) {
      const target = selectMap.value[key];
      params.costIds = target.map((t: any) => t.id) || [];
    }
    if (params.costIds.length === 0) return message.error("请先勾选费用!");
    Modal.confirm({
      title: "是否移出已勾选的费用?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        shiftOrOutCostApi(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("删除成功!");
            searchHandle();
          }
        });
      }
    });
  };
  // 移入费用
  const addFeeHandle = () => {
    dialogOpts.title = "移入费用";
    dialogOpts.width = "1200px";
    dialogOpts.type = 1;
    dialogOpts.passData = passData.value;
    dialogOpts.component = "AddFee";
    dialogOpts.visible = true;
  };
  return {
    submitHandle,
    searchHandle,
    selectChangeEvent,
    outCostHandle,
    selectAllEvent,
    addFeeHandle
  };
}
